﻿package com.jfdesgagne.jfdesgagne09.shell.view.component {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.display.Sprite;
	import flash.text.TextFieldAutoSize;
	import flash.display.MovieClip;
	import flash.display.BitmapData;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import gs.*; 
	import gs.easing.*;
	import com.jfdesgagne.utils.event.StringEvent;
	import com.jfdesgagne.jfdesgagne09.shell.view.event.TooltipEvent;
	
	public class Frame extends Sprite {
		public var logo_mc:MovieClip;
		private var _top_sp:Sprite;
		private var _topBg_mc:MovieClip;
		private var _mainMenu_sp:Sprite;
		private var _bottom_sp:Sprite;
		private var _bottomMenu_sp:Sprite;
		private var _bottomBg_mc:MovieClip;
		private var _copyright_mc:MovieClip;
		
		public static const MAINMENU_CLICK	 	= 'mainMenuClick';
		public static const BOTTOMMENU_CLICK 	= 'bottomMenuClick';
		public static const SHOW_TOOLTIP		= 'showTooltip';
			
		public function Frame() {}
		
		public function init():void {
			this.initTop();
			this.initMainMenu();
			this.initBottom();
			this.initBottomMenu();
		}
		
		private function initBottom():void {
			this._bottom_sp = new Sprite();
			this.addChild(this._bottom_sp);
			
			this._bottomBg_mc = new ShadowBottomMc();
			this._bottomBg_mc.width = this.stage.stageWidth;
			this._bottom_sp.addChild(this._bottomBg_mc);
			this._bottom_sp.y = this.stage.stageHeight - this._bottom_sp.height;
			
			this._copyright_mc = new CopyrightMc();
			this._bottom_sp.addChild(this._copyright_mc);
			this._copyright_mc.y = this._bottom_sp.height - this._copyright_mc.height - 10;
			this._copyright_mc.x = 30;
		}
		
		private function initBottomMenu():void {
			this._bottomMenu_sp = new Sprite();
			this._bottom_sp.addChild(this._bottomMenu_sp);
			
		}
		
		public function createBottomMenuItem(name:String, label:String, tooltip:String, letter:String, border:Boolean=false):void {
			var itemBg_mc:MovieClip = new MovieClip();
				itemBg_mc.alpha = 0;
				itemBg_mc.graphics.beginFill(0xfff600, 1);
				itemBg_mc.graphics.drawRect(0, 0, 1, 1);
				itemBg_mc.graphics.endFill();
			
			var item_mc:MovieClip = new BottomMenuMc();
				item_mc.letter_txt.text 	= letter;
				item_mc.label_txt.htmlText 	= label;
				item_mc.label_txt.autoSize	= TextFieldAutoSize.LEFT;
				item_mc.letter = letter;
				item_mc.itemName = name;
				item_mc.tooltip = tooltip;
				
			if(this._bottomMenu_sp.numChildren>0) item_mc.x = this._bottomMenu_sp.getChildAt(this._bottomMenu_sp.numChildren-1).x + this._bottomMenu_sp.getChildAt(this._bottomMenu_sp.numChildren-1).width+30; 
			this._bottomMenu_sp.addChild(itemBg_mc);
			this._bottomMenu_sp.addChild(item_mc);
			
			if(border) {
				this._bottomMenu_sp.graphics.lineStyle(1, 0x4d4d4d);
				this._bottomMenu_sp.graphics.moveTo(item_mc.x+item_mc.width+15, 5);
				this._bottomMenu_sp.graphics.lineTo(item_mc.x+item_mc.width+15, 20);
				this._bottomMenu_sp.graphics.endFill();
			}
			
			item_mc.graphics.beginFill(0xfff600, 0);
			item_mc.graphics.drawRect(0, 0, item_mc.width, item_mc.height);
			item_mc.graphics.endFill();
			item_mc.buttonMode = true;
			item_mc.mouseChildren = false;
			item_mc.initWidth 		= item_mc.width;
			item_mc.itemBg_mc 		= itemBg_mc;
			itemBg_mc.x = item_mc.x;
			itemBg_mc.y = item_mc.y;
			
			item_mc.addEventListener(MouseEvent.ROLL_OVER, this._bottomMenuItemRollOver);
			item_mc.addEventListener(MouseEvent.ROLL_OUT, this._bottomMenuItemRollOut);
			item_mc.addEventListener(MouseEvent.CLICK, this._bottomMenuItemClick);
		}
		
		private function initTop():void {
			this._top_sp = new Sprite();
			this.addChild(this._top_sp);
			
			this._topBg_mc = new ShadowTopMc();
			this._topBg_mc.width = this.stage.stageWidth;
			this._top_sp.addChild(this._topBg_mc);
			
			this.logo_mc = new LogoMc();
			this.logo_mc.subTitle_txt.autoSize = TextFieldAutoSize.LEFT;
			this.logo_mc.x = 30;
			this.logo_mc.y = 10;
			this.logo_mc.buttonMode = true;
			this.logo_mc.mouseChildren = false;
			this._top_sp.addChild(this.logo_mc);	
		}
		
		private function initMainMenu():void {
			this._mainMenu_sp = new Sprite();
			this._mainMenu_sp.y = 20;
			this.addChild(this._mainMenu_sp);
		}
		
		public function createMainMenuItem(name:String, label:String, subLabel:String, tooltip:String, letter:String, email:String=null):void {
			var itemBg_mc:MovieClip = new MovieClip();
				itemBg_mc.alpha = 0;
				itemBg_mc.graphics.beginFill(0xfff600, 1);
				itemBg_mc.graphics.drawRect(0, 0, 1, 1);
				itemBg_mc.graphics.endFill();
			
			var item_mc:MovieClip = new MainMenuMc();
				item_mc.label_txt.text 	= label;
				item_mc.label_txt.autoSize	= TextFieldAutoSize.LEFT;
				item_mc.letter_txt.text 	= letter;
				item_mc.letter = letter;
				item_mc.itemName = name;
				item_mc.tooltip = tooltip;
			
			if(this._mainMenu_sp.numChildren>0) item_mc.x = this._mainMenu_sp.width+10; 
			this._mainMenu_sp.addChild(itemBg_mc);
			this._mainMenu_sp.addChild(item_mc);
			
			if(email!='') {
				item_mc.email_txt.alpha = 1;
				item_mc.email_txt.autoSize = TextFieldAutoSize.LEFT;
				item_mc.email_txt.text = email.toUpperCase();
				item_mc.email_txt.width = 100;
			} else {
				item_mc.email_txt.alpha = 0;
				item_mc.label2_txt.text 	= subLabel;
				item_mc.label2_txt.autoSize	= TextFieldAutoSize.LEFT;
				
				this._mainMenu_sp.graphics.lineStyle(1, 0x4d4d4d);
				this._mainMenu_sp.graphics.moveTo(item_mc.x+item_mc.width+10, 5);
				this._mainMenu_sp.graphics.lineTo(item_mc.x+item_mc.width+10, 30);
				this._mainMenu_sp.graphics.endFill();
			}
			
			item_mc.graphics.beginFill(0xfff600, 0);
			item_mc.graphics.drawRect(0, 0, item_mc.width, item_mc.height);
			item_mc.graphics.endFill();
			item_mc.buttonMode 		= true;
			item_mc.mouseChildren 	= false;
			item_mc.initWidth 		= item_mc.width;
			item_mc.itemBg_mc 		= itemBg_mc;
			itemBg_mc.x = item_mc.x;
			itemBg_mc.y = item_mc.y;
				
			item_mc.addEventListener(MouseEvent.ROLL_OVER, this._mainMenuItemRollOver);
			item_mc.addEventListener(MouseEvent.ROLL_OUT, this._mainMenuItemRollOut);
			item_mc.addEventListener(MouseEvent.CLICK, this._mainMenuItemClick);
		}
		
		private function _mainMenuItemClick(event:MouseEvent):void {
			this.dispatchEvent(new StringEvent(MAINMENU_CLICK, event.target.itemName));
		}
		
		private function _bottomMenuItemClick(event:MouseEvent):void {
			this.dispatchEvent(new StringEvent(BOTTOMMENU_CLICK, event.target.itemName));
		}
		
		private function _mainMenuItemRollOver(event:MouseEvent):void {
			var clip_mc:MovieClip = event.target as MovieClip;
				clip_mc.itemBg_mc.alpha = 1;
				clip_mc.itemBg_mc.x = clip_mc.x-5;
				clip_mc.itemBg_mc.y = 4;
				clip_mc.itemBg_mc.height = 27;
				clip_mc.itemBg_mc.width = 0;
			TweenLite.to(clip_mc.itemBg_mc, 0.5, {width:clip_mc.initWidth+10, ease:Expo.easeOut});
			TweenLite.to(clip_mc, 0.2, { tint:0x000000, ease:Back.easeOut } );
			
			this.dispatchEvent(new TooltipEvent(TooltipEvent.SHOW, clip_mc, clip_mc.tooltip, clip_mc.letter));
		}
		
		private function _mainMenuItemRollOut(event:MouseEvent):void {
			var clip_mc:MovieClip = event.target as MovieClip;
			TweenLite.to(clip_mc, 0.1, {removeTint:true, ease:Back.easeOut});
			TweenLite.to(clip_mc.itemBg_mc, 0.1, { width:0, ease:Expo.easeOut } );
			this.dispatchEvent(new TooltipEvent(TooltipEvent.HIDE, clip_mc));
		}
		
		private function _bottomMenuItemRollOver(event:MouseEvent):void {
			var clip_mc:MovieClip = event.target as MovieClip;
				clip_mc.itemBg_mc.alpha = 1;
				clip_mc.itemBg_mc.x = clip_mc.x-5;
				clip_mc.itemBg_mc.y = 2;
				clip_mc.itemBg_mc.height = 22;
				clip_mc.itemBg_mc.width = 0;
			TweenLite.to(clip_mc.itemBg_mc, 0.5, {width:clip_mc.initWidth+10, ease:Expo.easeOut});
			TweenLite.to(clip_mc, 0.2, { tint:0x000000, ease:Back.easeOut } );
			
			this.dispatchEvent(new TooltipEvent(TooltipEvent.SHOW, clip_mc));
		}
		
		private function _bottomMenuItemRollOut(event:MouseEvent):void {
			var clip_mc:MovieClip = event.target as MovieClip;
			TweenLite.to(clip_mc, 0.1, {removeTint:true, ease:Back.easeOut});
			TweenLite.to(clip_mc.itemBg_mc, 0.1, { width:0, ease:Expo.easeOut } );
			this.dispatchEvent(new TooltipEvent(TooltipEvent.HIDE, clip_mc));
		}
		
		public function resize():void {
			this._bottomMenu_sp.y = this._bottom_sp.height - this._bottomMenu_sp.height - 10;
			
			this._bottomBg_mc.width = this.stage.stageWidth;
			this._bottom_sp.y = this.stage.stageHeight - this._bottom_sp.height;
			this._topBg_mc.width = this.stage.stageWidth;
			
			TweenLite.to(this._bottomMenu_sp, 1, {x:this.stage.stageWidth - this._bottomMenu_sp.width - 30, ease:Expo.easeOut});
			TweenLite.to(this._mainMenu_sp, 1, {x:this.stage.stageWidth - this._mainMenu_sp.width - 30, ease:Expo.easeOut});
		}
	}
}